<?php

namespace app\proxyapi\controller;

use app\data\model\LiveAppproxy;
use app\data\model\LiveIntegarlThali;
use app\data\model\LiveIntegralOrder;
use app\data\model\LiveProxyList;
use app\data\service\CreateUserService;
use think\admin\extend\CodeExtend;

class ManageOrder extends BaseProxyController
{
    //积分套餐列表
    public function getInteThaliList(){
        $userId = $this->checkTokenReturnUserId();
        $post = $this->request->post();
        if(!isset($post['appName'], $post['packageName'], $post['versionName'], $post['versionCode'])) $this->error('缺少参数！');
        $app = LiveAppproxy::mk()->where(['app_name'=>$post['appName'],'app_package'=>$post['packageName'],'deleted'=>0])->find();
        if(empty($app)) $this->error('不存在当前应用！');

        $proxy = LiveProxyList::mk()->where(['id'=>$userId])->find();
        //授权类型 1代理 2贴牌
        if($proxy['user_autotype'] == 1){
            $thalis = LiveIntegarlThali::mk()
                ->field('inte_id,inte_thali_name,proxy_rate as rate,proxy_price as price')
                ->where(['deleted'=>0])->select()->toArray();
        }else{
            $thalis = LiveIntegarlThali::mk()
                ->field('inte_id,inte_thali_name,label_rate as rate,label_price as price')
                ->where(['deleted'=>0])->select()->toArray();
        }
        $this->success('success',$thalis);
    }
    //订单列表
    public function getOrderList(){
        $userId = $this->checkTokenReturnUserId();
        $orders = LiveIntegralOrder::mk()
            ->field('order_no,inte_thaliid,order_status,order_amount,create_time')
            ->where(['proxy_id'=>$userId,'deleted'=>0])->select()->toArray();
        $thalis = LiveIntegarlThali::mk()->select()->toArray();
        foreach ($orders as &$order){
            $order['inte_thali_name'] = '';
            foreach ($thalis as $thali){
                if($thali['inte_id'] == $order['inte_thaliid']){
                    $order['inte_thali_name'] = $thali['inte_thali_name'];
                    break;
                }
            }
        }
        $this->success('success',$orders);
    }
    //创建订单
    public function createOrder(){
        $userId = $this->checkTokenReturnUserId();
        $post = $this->request->post();
        if(!isset($post['inte_id'],$post['app_id'])) $this->error('缺少参数！');
        $thali = LiveIntegarlThali::mk()->where(['inte_id'=>$post['inte_id']])->find();
        if(empty($thali)) $this->error('不存在该套餐！');

        $proxy = LiveProxyList::mk()->where(['id'=>$userId])->find();
        //user_autotype 授权类型 1代理 2贴牌
        if($proxy['user_autotype'] == 1){
            $order_amount = $thali['proxy_price'];
        }else{
            $order_amount = $thali['label_price'];
        }
        //创建订单
        LiveIntegralOrder::mk()->insert([
            'app_id'            =>$post['app_id'],
            'proxy_id'          =>$userId,
            'inte_thaliid'      =>$post['inte_id'],
            'order_no'          =>CodeExtend::uniqidDate(18, 'DH'),
            'order_status'      =>1,//订单状态(1预订单,2已支付,3已取消)
            'order_amount'      =>$order_amount,
            'examine_status'    =>0,//审核状态 0未审核 1审核通过
        ]);
        $this->success('创建成功');
    }
    //取消订单
    public function cancelOrder(){
        $userId = $this->checkTokenReturnUserId();
        $post = $this->request->post();
        if(!isset($post['order_no'])) $this->error('缺少参数！');
        $order = LiveIntegralOrder::mk()->where(['order_no'=>$post['order_no'],'proxy_id'=>$userId])->find();
        if(empty($order)) $this->error('不存在该订单');
        if($order['order_status'] != 1) $this->error('该订单已支付或已取消，不可操作');
        if($order['examine_status'] != 0) $this->error('该订单已审核通过，不可操作');

        LiveIntegralOrder::mk()->where(['order_no'=>$post['order_no'],'proxy_id'=>$userId])->update(['order_status'=>3]);
        $this->success('取消成功');
    }
    //删除订单
    public function deleteOrder(){
        $userId = $this->checkTokenReturnUserId(); $post = $this->request->post();
        if(!isset($post['order_no'])) $this->error('缺少参数！');
        $order = LiveIntegralOrder::mk()->where(['order_no'=>$post['order_no'],'proxy_id'=>$userId])->find();
        if(empty($order)) $this->error('不存在该订单');
        if($order['order_status'] == 1) $this->error('请先已取消该订单，再执行删除操作');

        LiveIntegralOrder::mk()->where(['order_no'=>$post['order_no'],'proxy_id'=>$userId])->update(['deleted'=>1]);
        $this->success('删除成功');

    }
}